/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.service.converter;

import java.util.HashSet;
import java.util.Set;

import pa165.dominating.species.entities.Animal;
import pa165.dominating.species.tos.AnimalTo;

/**
 *
 * @author Filip Dinh
 */
public class AnimalConverter {

    private EnvironmentConverter environmentConverter = new EnvironmentConverter();

    public Animal convertAnimalTo2Animal(AnimalTo animalTo) {
        Animal animal = new Animal();
        animal.setId(animalTo.getId());
        animal.setName(animalTo.getName());
        animal.setSpecies(animalTo.getSpecies());
        animal.setEnvironment(environmentConverter.convertEnvironmentTo2Environment(animalTo.getEnvironmentTo()));
        animal.setPrey(convert2Animals(animalTo.getPreyTo()));
        return animal;
    }

    public AnimalTo convertAnimal2AnimalTo(Animal animal) {
        AnimalTo animalTo = new AnimalTo();
        animalTo.setId(animal.getId());
        animalTo.setName(animal.getName());
        animalTo.setSpecies(animal.getSpecies());
        animalTo.setEnvironmentTo(environmentConverter.convertEnvironment2EnvironmentTo(animal.getEnvironment()));
        animalTo.setPreyTo(convert2AnimalsTo(animal.getPrey()));
        return animalTo;
    }
    
    public AnimalTo convertAnimal2AnimalToWithoutPrey(Animal animal) {
        AnimalTo animalTo = new AnimalTo();
        animalTo.setId(animal.getId());
        animalTo.setName(animal.getName());
        animalTo.setSpecies(animal.getSpecies());
        animalTo.setEnvironmentTo(environmentConverter.convertEnvironment2EnvironmentTo(animal.getEnvironment()));
        animalTo.setPreyTo(new HashSet<AnimalTo>());
        return animalTo;
    }

    public Set<AnimalTo> convert2AnimalsTo(Set<Animal> animals) {
        final Set<AnimalTo> animalTos = new HashSet<>();
        for (Animal animal : animals) {
            animalTos.add(convertAnimal2AnimalToWithoutPrey(animal));
        }
        return animalTos;
    }

    public Set<Animal> convert2Animals(Set<AnimalTo> animalsTos) {
        final Set<Animal> animals = new HashSet<>();
        for (AnimalTo animalTo : animalsTos) {
            animals.add(convertAnimalTo2Animal(animalTo));
        }
        return animals;
    }
}
